package com.itcast.googleplay.fragment;

import java.util.List;
import java.util.Random;

import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import com.itcast.googleplay.protocol.RecommendProtocol;
import com.itcast.googleplay.ui.LoadingPage.ResultState;
import com.itcast.googleplay.ui.randomlayout.StellarMap;
import com.itcast.googleplay.utils.UIUtils;


public class RecommendFragment extends BaseFragment {
	private List<String> recommendData;

	@Override
	public ResultState onSubLoad() {
		RecommendProtocol recommendProtocol = new RecommendProtocol();
		recommendData = recommendProtocol.getData("recommend", 0, "");
		if(recommendData!=null){
			if(recommendData.size()>0){
				return ResultState.STATE_SUCCESSED;
			}
			return ResultState.STATE_EMPTY;
		}
		return ResultState.STATE_ERROR;
	}

	@Override
	public View onCreateSubSuccessedView() {
		//1.创建飞入飞出控件
		StellarMap stellarMap = new StellarMap(UIUtils.getContext());
		//2.给飞入飞出的控件指定内间距
		int padding = UIUtils.dip2px(10);
		stellarMap.setInnerPadding(padding, padding, padding, padding);
		//3.给stellarMap设置一个数据适配器
		MyAdapter myAdapter = new MyAdapter();
		stellarMap.setAdapter(myAdapter);
		//4.设置飞入飞出显示规则xRegularity+yRegularity = 数据适配器中getCount方法的返回结果
		stellarMap.setRegularity(recommendData.size()-10, 10);
		//5.设置起始动画
		stellarMap.setGroup(0, true);
		
		return stellarMap;
	}
	
	class MyAdapter implements StellarMap.Adapter{
		@Override
		public int getGroupCount() {
			//返回组方法,动画的组数(飞入	飞出)
			return 2;
		}

		@Override
		public int getCount(int group) {
			return recommendData.size();
		}

		@Override
		public View getView(int group, int position, View convertView) {
			//在此方法中维护飞入飞出文本条目
			TextView textView = new TextView(UIUtils.getContext());
			textView.setText(recommendData.get(position));
			
			//1.随机文字大小
			textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16+new Random().nextInt(23));
			//2.随机颜色(三原色构成,随机三原色中的红,绿,蓝三个色值就能得到随机色)(#ff(0-255) ff(0-255) ff(0-255))
			int redRandom = 30+new Random().nextInt(210);		
			int greenRandom = 30+new Random().nextInt(210);	
			int bleuRandom = 30+new Random().nextInt(210);		
			//3.够三原色
			int rgb = Color.rgb(redRandom, greenRandom, bleuRandom);
			textView.setTextColor(rgb);
			
			return textView;
		}

		@Override
		public int getNextGroupOnPan(int group, float degree) {
			return 0;
		}

		@Override
		public int getNextGroupOnZoom(int group, boolean isZoomIn) {
			return 0;
		}
	}
}
